package com.atguigu.eduservice.controller;


import com.atguigu.common_uitls.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *  LTAI5tKxP4souv38QJwTx86r
 *  5geIzjYdenfp8FJP90VXJwgkIwmpY4
 * @author wzh
 * @since 2022-09-17
 */
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;

    @GetMapping("findAll")
    public R findAllTeacher(){
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items",list);
    }
    //逻辑删除讲师
    @DeleteMapping("{id}")
    public R deleteTeacher(@PathVariable String id){
        boolean remove = eduTeacherService.removeById(id);
        if (remove)
            return R.ok();
        else
            return R.error();
    }
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageTeacher(@PathVariable long current,@PathVariable long limit){
        Page<EduTeacher> page=new Page<>(current,limit);
        //调用方法实现分页
        eduTeacherService.page(page,null);
        List<EduTeacher> records = page.getRecords();
        long total = page.getTotal();
        return R.ok().data("total",total).data("rows",records);
    }
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){
        Page<EduTeacher> page =new Page<>(current,limit);
        QueryWrapper<EduTeacher> queryWrapper=new QueryWrapper<>();
        eduTeacherService.page(page,queryWrapper);
        String name = teacherQuery.getName();
        String begin = teacherQuery.getBegin();;
        String end = teacherQuery.getEnd();
        Integer level = teacherQuery.getLevel();
        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level)){
            queryWrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(begin)){
            queryWrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)){
            queryWrapper.le("gmt_create",end);
        }
        queryWrapper.orderByDesc("gmt_create");
        List<EduTeacher> records = page.getRecords();
        long total = page.getTotal();
        return R.ok().data("total",total).data("rows",records);
    }
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = eduTeacherService.save(eduTeacher);
        if (save){
            return R.ok();
        }
        else
            return R.error();
    }
    //根据id查询
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        EduTeacher teacher = eduTeacherService.getById(id);
        return R.ok().data("teacher",teacher);
    }
    @PostMapping("updateTeacher")
    public R uodateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if (flag){
            return R.ok();
        }
        else
            return R.error();
    }
}

